import router from './router';
import store from './store';
import { Message } from 'element-ui';
import { getToken } from '@/utils/auth.js';
const whiteList = ['/login', '/mapTest', '/mapTest1', '/digitalScene', '/yaoantongBigScreen', '/digitalScene1', '/lodop', '/specialPrint'];
router.beforeEach(async(to, from, next) => {
  const id = localStorage.getItem('id');
  const roleId = getToken('xcRoleId');
  const token = getToken('xcToken');
  const isCanGoto = getToken('isCanGoto');
  if (token && isCanGoto) {
    if (to.path === '/login' || to.path === '/') {
      let gotoUlr = '';

      gotoUlr = getToken('pcUrlName');
      next({ path: gotoUlr });
    } else {
      if (id !== '1') {
        if (localStorage.getItem('pcUrlName') !== '/layoutContainer' && localStorage.getItem('pcUrlName') !== '/digitalScene') {
          if (localStorage.getItem('pcUrlName') === '/special/home' && localStorage.getItem('specialPanorama') !== '') {
            await store.commit('permission/specialChangeRoutes', to.query.parentTitle);
          } else if (localStorage.getItem('pcUrlName') === '/yaoantongBigScreen') {
          } else {
            const addressUrl = to.path;
            const result = addressUrl.split('/')[1];
            await store.commit('permission/changeRoutes', result);
          }
        }
        localStorage.setItem('siderName', to.meta.title);
        next();
      } else {
        try {
          const accessRoutes = await store.dispatch('permission/generateRoutes', roleId);
          accessRoutes.push({
            path: '/layoutContainer',
            redirect: accessRoutes[0].redirect
          });
          router.addRoutes(accessRoutes);
          localStorage.setItem('id', '2');
          next({ ...to, replace: true });
        } catch (error) {
          Message.error(error.message || 'Has Error');
          next('/login');
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next('/login');
    }
  }
});
router.afterEach(() => {
});
